package org.example.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ ClassName: DatabaseController
 * @ Description：
 * @ Modified By：
 * @ Version:
 * @ Author     ：LiYing
 * @ Date       ：Created in 16:41 2022/4/23
 */

@Controller
@RequestMapping(value = "/database")
public class DatabaseController {
    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = "/delete")
    public @ResponseBody
    boolean delete(String tableName) {
        Boolean forObject = restTemplate.getForObject("http://EUREKA-SERVICE/database/delete?tableName=" + tableName, Boolean.class);
        return forObject;
    }

    @RequestMapping(value = "/create")
    public @ResponseBody
    boolean create(String sql) {
        Boolean forObject = restTemplate.getForObject("http://EUREKA-SERVICE/database/create?sql=" + sql, Boolean.class);
        return forObject;
    }

    @RequestMapping(value = "/tableinfo")
    public @ResponseBody
    Map getTableInfo(String tableName) {
        Map map = restTemplate.getForObject("http://EUREKA-SERVICE/database/tableinfo?tableName=" + tableName, Map.class);
        return map;
    }

    @RequestMapping(value = "/updatecolumn")
    public @ResponseBody
    String updateColumnValueByKey(String tableName, String columnName, String key, Integer keyId, Object value) {
        String s = restTemplate.getForObject("http://EUREKA-SERVICE/database/updatecolumn?tableName=" + tableName + "?columnName=" + columnName + "?key=" + key + "?keyId=" + keyId + "?value=" + value, String.class);
        return s;
    }

    @RequestMapping(value = "/sql")
    public @ResponseBody
    List getIdNameSelectList(String sql) {
        List list = restTemplate.getForObject("http://EUREKA-SERVICE/database/sql?sql=" + sql, List.class);
        return list;
    }

    @RequestMapping(value = "/createbyexcel")
    /*
     * will be not used
     * @param file
     * @return java.lang.String
     * @author LiYing
     * @creed: Talk is cheap,show me the code
     * @date 2022/4/23 17:04
     */
    public @ResponseBody
    String createTableByExcel(@RequestParam("file") MultipartFile file) {
        File file1 = null;
        try {
            HttpEntity<MultiValueMap<String, Object>> httpEntity;
            String responseEntity = restTemplate.getForObject("http://EUREKA-SERVICE/database/importExcel", String.class);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != file) {
                //最后要删除
                file1.delete();
            }
        }
        return "result";
    }

    @RequestMapping(value = "/tablenames")
    public @ResponseBody
    List getTableNames() {
        List list = restTemplate.getForObject("http://EUREKA-SERVICE/database/tablenames", List.class);
        return list;
    }

    @RequestMapping(value = "/insertsql")
    public @ResponseBody String insertDataBySql(String sql) {
        String string = restTemplate.getForObject("http://EUREKA-SERVICE/database/insertsql?sql=" + sql, String.class);
        return string;
    }

}
